function TreeNode(val, left, right) {
  this.val = val === undefined ? 0 : val;
  this.left = left === undefined ? null : left;
  this.right = right === undefined ? null : right;
}

const root = new TreeNode(
  1,
  new TreeNode(2, new TreeNode(3), new TreeNode(4)),
  new TreeNode(2, null, null)
);
// 简单解法
// var maxDepth = function(root) {
//     if(!root)
//       return 0;
//     else {
//       const l = maxDepth(root.left);
//       const r = maxDepth(root.right);
//       return Math.max(l, r) + 1;
//     }
//   };

console.log("root: ", root);
var maxDepth = function (root) {
  let depth = 0;
  if (!root) return depth;
  const getDepth = (node, n) => {
    if (!node) return;
    depth = Math.max(depth, n);
    getDepth(node.left, n + 1);
    getDepth(node.right, n + 1);
  };
  getDepth(root, 1);
  return depth;
};

const ans = maxDepth(root);
console.log("ans: ", ans);
console.log();
